﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Pages/admin/list_view.master" %>

<script runat="server">
    void Page_Init(object o, EventArgs e)
    {
        if (null != Session["BranchID"])
        {
            ds2.FilterExpression = string.Format("BranchID={0}", Session["BranchID"]);
        }
        else
        {
            ds2.FilterExpression = string.Format("BranchID={0}", 0);
        }
        if (!IsPostBack)
        {
            ds2.FilterExpression = string.Format("BranchID={0}", 0);
        }
    }
    
    void clickme(object o, EventArgs e)
    {
        //System.Threading.Thread.Sleep(5000);
       // mylabel.Text = "click alrady";
       // timerbox.Text = DateTime.Now.ToString();
        
    }

    void MakeChanged(Object sender,  EventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        int t = grid.FocusedRowIndex;
        if (t > -1)
        {
            object id = grid.GetRowValues(t, new string[] { "BranchID" });
            //object name = grid.GetRowValues(t, new string[] { "Name" });
            // mylabel.Text = "Selection:" + id.ToString();
            
            Session["BranchID"] = id;
            int idn = int.Parse(id.ToString());
            if (idn > 0)
            {
                int cnt = (int)Touchmax.Manager.ORManager.ExecuteScalar("Select count(*) from FaRate where BranchID=" + id.ToString());
                if (cnt == 0)
                {
                    // auto load
                    DataSet ds = Touchmax.Manager.ORManager.GetDataSet("Select ItemID from FaItem");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        Touchmax.FaRate fr = new FaRate();
                        fr.BranchID = idn;
                        fr.ChargeID = int.Parse(ds.Tables[0].Rows[i]["ItemID"].ToString());
                        fr.Rate1 = 0;
                        fr.Rate2 = 0;
                        fr.Rate3 = 0;
                        fr.Rate4 = 0;
                        fr.DateFrom = new DateTime(DateTime.Today.Year, 1, 1);
                        fr.DateTo = new DateTime(DateTime.Today.Year, 12, 31);
                        Touchmax.Manager.ORManager.StartTracking(fr, Wilson.ORMapper.InitialState.Inserted);
                        Touchmax.Manager.ORManager.PersistChanges(fr);
                    }
                }
            } 
            
            ds2.FilterExpression = string.Format("BranchID={0}", Session["BranchID"]);

            grid2.Selection.UnselectAll();
            grid2.PageIndex = 0;
            grid2.DataBind();
            
            
            
            
            //this.dsXXModel.FilterExpression = "BranchID=" + o;
            //DataBind();
        }
    }


    protected void grid2_Init(object sender, EventArgs e)
    {
        ASPxGridView grd = sender as ASPxGridView;
        if (grd != null)
        {
            grd.ForceDataRowType(typeof(Touchmax.DdItem));
        }
    }

    protected void grid2_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
    }

    protected void grid2_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
    }

    protected void grid2_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        int id = int.Parse(Session["BranchID"].ToString());
        e.NewValues["BranchID"] = id;
        e.NewValues["Status"] = "Active";
        e.NewValues["Sort"] = 100;
    }

    
    </script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style>
    div {font-size:10px;font-family:Verdana;}
    </style>
    <script type="text/javascript">
    </script>
</asp:Content>

<asp:Content ID="title1" ContentPlaceHolderID="Title1" Runat="Server">
<table width=100% cellpadding="4">
    <tr>
    <td align=left class="bb">
        Branch List
    </td>
    <td align=right class="bb">
    </td>
    </tr>
</table>
</asp:Content>
<asp:Content ID="title2" ContentPlaceHolderID="Title2" Runat="Server">
<table width=100% cellpadding="4">
    <tr>
    <td align=left class="bb">
        Branch Master Price Plan
    </td>
    <td align=right class="bb">
       
    </td>
    </tr>
</table>
</asp:Content>
<asp:Content ID="title3" ContentPlaceHolderID="Title3" Runat="Server">

</asp:Content>

<asp:Content ID="Page1" ContentPlaceHolderID="Page1" Runat="Server">
<wilson:DataSource ID="ds1" TypeName="Touchmax.BoBranch" KeyMember="BranchID" FilterExpression="1=1" runat="server" ObjectSpace="Touchmax.Manager.ORManager"/>
<dxwgv:ASPxGridView ID="grid1" ClientInstanceName="grid1" runat="server" DataSourceID="ds1" KeyFieldName="BranchID" 
       OnFocusedRowChanged="MakeChanged" Width="100%" AutoGenerateColumns="False" EnableCallBacks="False">
                <SettingsPager Mode="ShowAllRecords"/>
                <Columns>
                    <dxwgv:GridViewDataColumn FieldName="BranchName" VisibleIndex="1" SortIndex="1" SortOrder="Ascending">
                    </dxwgv:GridViewDataColumn>
                </Columns>
                <Settings ShowGroupPanel="False" ShowFooter="False" ShowTitlePanel="false" ShowColumnHeaders="False" />
                <SettingsBehavior AllowMultiSelection="false" AllowFocusedRow="true" ProcessFocusedRowChangedOnServer="true" />
            </dxwgv:ASPxGridView>
</asp:Content>
<asp:Content ID="Page2" ContentPlaceHolderID="Page2" Runat="Server">

<wilson:DataSource ID="dsBranch" TypeName="Touchmax.BoBranch" KeyMember="BranchID" FilterExpression="1=1" runat="server" ObjectSpace="Touchmax.Manager.ORManager"/>
<wilson:DataSource ID="dsCombo" TypeName="Touchmax.FaItem" KeyMember="ItemID" FilterExpression="1=1" runat="server" ObjectSpace="Touchmax.Manager.ORManager"/>
<wilson:DataSource ID="ds2" TypeName="Touchmax.FaRate" KeyMember="RateID" FilterExpression="BranchID=0" runat="server" ObjectSpace="Touchmax.Manager.ORManager"/>
<dxwgv:ASPxGridView ID="grid2" ClientInstanceName="grid2" runat="server" DataSourceID="ds2" KeyFieldName="RateID" 
        Width="100%" AutoGenerateColumns="False"  EnableCallBacks="False" OnInit="grid2_Init" OnInitNewRow="grid2_InitNewRow" OnRowUpdating="grid2_RowUpdating" OnRowInserting="grid2_RowInserting">
                <SettingsPager Mode="ShowAllRecords"/>
                <Columns>
                    <dxwgv:GridViewCommandColumn VisibleIndex="0" Width="80">
                        <EditButton Visible="True" />
                    </dxwgv:GridViewCommandColumn>           
                    <dxwgv:GridViewDataComboBoxColumn Caption="Charge Code" ReadOnly="true" FieldName="ChargeID" VisibleIndex="1">
                        <PropertiesComboBox DataSourceID="dsCombo" TextField="ItemName" ValueField="ItemID" EnableSynchronization="False"
                    EnableIncrementalFiltering="True">
                </PropertiesComboBox>
                    </dxwgv:GridViewDataComboBoxColumn>
                    <dxwgv:GridViewDataColumn Caption="Retail" FieldName="Rate1" VisibleIndex="2"></dxwgv:GridViewDataColumn>
                    <dxwgv:GridViewDataColumn Caption="Dealer" FieldName="Rate2" VisibleIndex="3"></dxwgv:GridViewDataColumn>
                    <dxwgv:GridViewDataColumn Caption="Other" FieldName="Rate3" VisibleIndex="4"></dxwgv:GridViewDataColumn>
                               
                    </Columns>
                    <SettingsEditing Mode="Inline" />
      
                <Settings ShowGroupPanel="False" ShowFooter="False" ShowTitlePanel="false" ShowColumnHeaders="True" />
                <SettingsBehavior ConfirmDelete="True" AllowFocusedRow="true" ProcessFocusedRowChangedOnServer="true" />
            </dxwgv:ASPxGridView>
</asp:Content>
<asp:Content ID="Page3" ContentPlaceHolderID="Page3" Runat="Server">
</asp:Content>

